package xin.xihc.jba.sql;

import xin.xihc.jba.scan.TableManager;

/**
 * Delete语句生成器
 *
 * @author Leo.Xi
 * @date 2020/9/22
 * @since 1.0
 **/
public class Delete implements SqlType {

    /** TABLE 表 */
    private String tableName;
    private KV kv = new KV("d");
    private Where where;

    private Delete() {
    }

    /**
     * 更新的表
     *
     * @param clazz 实体类
     * @return
     */
    public static Delete from(Class<?> clazz) {
        Delete update = new Delete();
        update.tableName = TableManager.getTable(clazz).getTableName();
        return update;
    }

    /**
     * 更新的表
     *
     * @param tableName 表名
     * @return
     */
    public static Delete from(String tableName) {
        Delete update = new Delete();
        update.tableName = tableName;
        return update;
    }

    /**
     * where表达式
     *
     * @return
     */
    public Where where() {
        if (this.where != null) {
            return this.where;
        }
        this.where = new Where(this.kv);
        return this.where;
    }

    public KV getKv() {
        return this.kv;
    }

    /**
     * 转为SQL语句
     *
     * @return sql
     * @author Leo.Xi
     * @date 2020/1/14
     * @since 0.0.1
     */
    @Override
    public String toSql() {
        StringBuilder sql = new StringBuilder(action());
        sql.append(this.tableName);
        if (this.where != null) {
            sql.append(this.where.toSql());
        }
        return sql.toString();
    }

    /**
     * 操作类型
     *
     * @return SELECT、WHERE、UPDATE、ORDER BY、
     * @author Leo.Xi
     * @date 2020/1/18
     * @since 0.0.1
     */
    @Override
    public String action() {
        return SqlConstants.DELETE + SqlConstants.FROM;
    }
}
